﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _17.责任链模式
{
    class Program
    {
        static void Main(string[] args)
        {
            PurchaseRequest requestTelphone = new PurchaseRequest(4000.0, "手机");
            PurchaseRequest requestSoftware = new PurchaseRequest(10000.0, "软件");
            PurchaseRequest requestComputers = new PurchaseRequest(40000.0, "电脑");

            Approver manager = new Manager("张三");
            Approver Vp = new VicePresident("李四");
            Approver Pre = new President("王五");

            manager.NextApprover = Vp;                // 设置责任链
            Vp.NextApprover = Pre;

            manager.ProcessRequest(requestTelphone); // 处理请求
            manager.ProcessRequest(requestSoftware);
            manager.ProcessRequest(requestComputers);

            Console.ReadLine();
 
        }
    }
    public class PurchaseRequest// 采购请求
    {

        public double Amount { get; set; }// 金额
        public string ProductName { get; set; }
        public PurchaseRequest(double amount, string productName)
        {
            Amount = amount;
            ProductName = productName;
        }
    }
    public abstract class Approver// 审批人,Handler
    {
        public Approver NextApprover { get; set; }
        public string Name { get; set; }
        public Approver(string name)
        {
            Name = name;
        }
        public abstract void ProcessRequest(PurchaseRequest request);
    }
    public class Manager : Approver
    {
        public Manager(string name) : base(name) { }
        public override void ProcessRequest(PurchaseRequest request)
        {
            if (request.Amount < 10000.0)
            {
                Console.WriteLine("{0}-{1}  想买  {2}", this, Name, request.ProductName);
            }
            else if (NextApprover != null)
            {
                NextApprover.ProcessRequest(request);
            }
        }
    }
    public class VicePresident : Approver// ConcreteHandler,副总
    {
        public VicePresident(string name) : base(name) { }
        public override void ProcessRequest(PurchaseRequest request)
        {
            if (request.Amount < 25000.0)
            {
                Console.WriteLine("{0}-{1}  想买  {2}", this, Name, request.ProductName);
            }
            else if (NextApprover != null)
            {
                NextApprover.ProcessRequest(request);
            }
        }
    }
    public class President : Approver// ConcreteHandler，总经理
    {
        public President(string name) : base(name) { }
        public override void ProcessRequest(PurchaseRequest request)
        {
            if (request.Amount < 100000.0)
            {
                Console.WriteLine("{0}-{1}  想买  {2}", this, Name, request.ProductName);
            }
            else
            {
                Console.WriteLine("Request需要组织一个会议讨论");
            }
        }
    }
 
}
